<template>
    <div>
        <div>
            <el-input size="small" style="width: 300px" v-model="jobLevel.name" placeholder="添加职称等级"
                      prefix-icon="el-icon-plus"></el-input>
            <el-select
                size="small"
                style="margin-left: 8px; margin-right: 8px"
                v-model="jobLevel.titleLevel"
                placeholder="请选择">
                <el-option
                    v-for="item in titleLevels"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
            </el-select>
            <el-button size="small" type="primary" icon="el-icon-plus" @click="addJobLevel">添加</el-button>
        </div>
        <div style="margin-top: 10px">
            <el-table
                :data="tableJobLevel"
                stripe
                border
                style="width: 70%"
                @selection-change="handleSelectionChang">
                <el-table-column
                    type="selection"
                    width="55">
                </el-table-column>
                <el-table-column
                    prop="id"
                    label="编号"
                    width="55">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="职称"
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="titleLevel"
                    label="职称等级"
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="createData"
                    label="创建日期"
                    width="150">
                </el-table-column>
                <el-table-column
                    prop="enabled"
                    label="是否启用"
                    width="150">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作">
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            @click="showEditView(scope.row)">编辑
                        </el-button>
                        <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-button size="small" type="danger" @click="deleteMany" :disabled="this.multipleSelection.length == 0">
                批量删除
            </el-button>
        </div>
        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%">
            <table>
                <tr>
                    <td>
                        <el-tag>职称名称</el-tag>
                    </td>
                    <td>
                        <el-input style="margin-left: 8px" size="small" v-model="updateJobLevel.name"></el-input>
                    </td>
                </tr>
                <tr>
                    <td>
                        <el-tag>职称等级</el-tag>
                    </td>
                    <td>
                        <el-select
                            size="small"
                            style="margin-left: 8px; margin-right: 8px"
                            v-model="updateJobLevel.titleLevel"
                            placeholder="请选择">
                            <el-option
                                v-for="item in titleLevels"
                                :key="item"
                                :label="item"
                                :value="item">
                            </el-option>
                        </el-select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <el-tag>是否启用</el-tag>
                    </td>
                    <td>
                        <el-switch
                            style="margin-left: 8px"
                            v-model="updateJobLevel.enabled"
                            active-text="已启用"
                            inactive-text="未启用"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                        </el-switch>
                    </td>
                </tr>
            </table>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
              </span>
        </el-dialog>
    </div>

</template>

<script>
    export default {
        name: 'JobLevelManager',
        data () {
            return {
                jobLevel: {
                    name: '',
                    titleLevel: '',
                    enabled: ''
                },
                updateJobLevel: {
                    name: '',
                    titleLevel: '',
                    enabled: ''
                },
                titleLevels: [
                    //先写死
                    '正高级',
                    '副高级',
                    '中级',
                    '初级',
                    '员级',
                ],
                tableJobLevel: [],
                dialogVisible: false,
                multipleSelection: [],
                disableDeleteStatus: false
            }
        },
        mounted () {
            this.initJobLevelTable()
        },
        methods: {
            initJobLevelTable () {
                this.getRequest('/system/basic/jobLevel').then(resp => {

                    this.tableJobLevel = resp
                    this.jobLevel.name = ''
                    this.jobLevel.titleLevel = ''

                })
            },
            addJobLevel () {
                if (this.jobLevel.name && this.jobLevel.titleLevel) {
                    this.postRequest('/system/basic/jobLevel', this.jobLevel).then(resp => {

                        this.initJobLevelTable()

                    })
                } else {
                    this.$message.error('字段不能为空')
                }
            },
            handleDelete (data) {
                this.$confirm('此操作将永久删除[' + data.name + ']职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/deleteJobLevel?ids=' + data.id).then(resp => {

                        this.initJobLevelTable()

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            },
            showEditView (data) {
                Object.assign(this.updateJobLevel, data)  //拷贝，防止同步显示，this.updateJob = data会同时显示修改
                this.dialogVisible = true
            },
            doUpdate () {
                if (this.updateJobLevel.name) {
                    this.putRequest('/system/basic/updateJobLevel', this.updateJobLevel).then(resp => {
                        this.initJobLevelTable()
                        this.dialogVisible = false
                    })
                } else {
                    this.$message.error('字段不能为空')
                }

            },
            handleSelectionChang (val) {
                this.multipleSelection = val
            },
            deleteMany () {
                this.$confirm('此操作将永久删除' + this.multipleSelection.length + '条职称, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?'
                    this.multipleSelection.forEach(value => {
                        ids = ids + 'ids=' + value.id + '&'
                    })
                    ids = ids.substr(0, ids.length - 1)
                    this.deleteRequest('/system/basic/deleteJobLevel' + ids).then(resp => {
                        this.initJobLevelTable()

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            }
        }
    }
</script>

<style scoped>

</style>
